
using System.Collections.Generic;
using System.Linq;
using M0;
using UnityEngine;
using UnityEngine.UI;

namespace M1
{
    public class Window管理器 : BaseSingleMono<Window管理器>
    {
        private Dictionary<string, BaseWindow> allWin = new Dictionary<string, BaseWindow>();
        private List<CompLayer> allLayer;

        protected override void AwakeInit()
        {
            base.AwakeInit();
            切换场景("");
            M.Event.AddEvent<string>(M.EventKey_切换场景,切换场景);
        }

        private void OnDestroy()
        {
            M.Event.RemoveEvent<string>(M.EventKey_切换场景,切换场景);
        }

        private void 切换场景(string s)
        {
            foreach (var VARIABLE in allWin)
            {
                GameObject.Destroy(VARIABLE.Value.gameObject);
            }
            allWin.Clear();
            allLayer = GameObject.FindObjectsOfType<CompLayer>().ToList();
        }

        public T GetWindow<T>() where T: BaseWindow
        {
            string name = typeof(T).Name;
            if (allWin.ContainsKey(name) && allWin[name] is T t)
            {
                return t;
            }
            return InitWindow<T>();
        }

        private BaseWindow GetWindow(string name)
        {
            if (allWin.ContainsKey(name))
            {
                return allWin[name];
            }
            return initWindow(name);
        }

        private T InitWindow<T>() where T: BaseWindow
        {
            string name = typeof(T).Name;
            var win = initWindow(name);
            
            return win as T;
        }

        private BaseWindow initWindow(string name)
        {
            string path = $"Window/{name}";
            var winobj = M.Resource.Load<GameObject>(path);
            if (winobj == null) return null;

            var win = winobj.GetComponent<BaseWindow>();
            int layer = win.Layer;
            var f = allLayer.FirstOrDefault(f => f.Layer == layer);
            if (f != null)
            {
                win.transform.SetParent(f.transform,false);
            }
            else
            {
                var canvas = FindObjectOfType<CanvasScaler>();
                if (canvas != null)
                {
                    win.transform.SetParent(canvas.transform,false);
                }
            }

            if (!allWin.ContainsKey(name))
            {
                allWin.Add(name, win);
            }
            return win;
        }

        public T OpenWindow<T>() where T: BaseWindow
        {
            T w = GetWindow<T>();
            w.OpenWindow();
            return w;
        }
        // public void OpenWindow<T>() where T: BaseWindow
        // {
        //     T w = GetWindow<T>();
        //     w.OpenWindow();
        // }
        public void OpenWindow(string win)
        {
            BaseWindow w = GetWindow(win);
            w.OpenWindow();
        }

        public CompLayer GetLayer(int 名片层级)
        {
            return allLayer.FirstOrDefault(f => f.Layer == 名片层级);
        }
    }

}
